package com.heeexy.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.service.StudentService;
import com.heeexy.example.util.CommonUtil;
import com.heeexy.example.util.OperLog;
import com.heeexy.example.util.constants.ErrorEnum;
import com.heeexy.example.util.file.ReadExcel;
import com.heeexy.example.util.model.response.PageResult;
import com.heeexy.example.util.model.response.QueryPageBean;
import com.heeexy.example.util.model.response.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     * logger
     */
    private Logger logger = LoggerFactory.getLogger(StudentController.class);

    @PostMapping("/importStudentXls")
    @OperLog(operDesc = "老师进行了学生表格的批量导入")
    public JSONObject importTeacherXls(MultipartFile file) throws IOException {
        return studentService.saveStudentXls(file);
    }
    @GetMapping("/outputStudentXls")
    public void outputTeacherXls(HttpServletResponse response,String c_id) {
        studentService.WriteTeacherXls(response,c_id);
    }
    /**
     * 查询学生列表
     */
//    @RequiresPermissions("user:list")
    @GetMapping("/findAll")
    public JSONObject findAll(String c_id) {
        return studentService.findAll(c_id);
    }

    /**
     * 分页
     */
    @PostMapping("/findPage")
    @OperLog(operDesc = "查询该班级下的所有学生")
    public JSONObject findPage(QueryPageBean queryPageBean, Integer s_slassid) {
        PageResult page = studentService.findPage(queryPageBean, s_slassid);
        return CommonUtil.successJson(page);
    }

    /*
     * 删除学生
     * */
    @OperLog(operDesc = "删除学生")
    @GetMapping("/deleteById/{sId}")
    public JSONObject deleteById(@PathVariable("sId") int sId) {
        int i = studentService.deleteById(sId);
        if (i > 0) {
            return CommonUtil.errorJson(ErrorEnum.E_10015);
        }
        return CommonUtil.errorJson(ErrorEnum.E_10021);
    }


    /*
     * 新增学生（学号）或外来人员（手机号当做学号）
     * */
    @PostMapping("/addStudent")
    @OperLog(operDesc = "新增学生")
    public JSONObject addStudent(@RequestBody JSONObject jsonObject) {
        return studentService.addStudent(jsonObject);
    }

    /*
     * 根据用户名/学号/手机号进行查询
     * */
    @GetMapping("/findQuerySnameOrSnumberOrStelephone")
    public JSONObject findQuerySnameOrSnumberOrStelephone(HttpServletRequest request) {
        return studentService.findQuerySnameOrSnumberOrStelephone(CommonUtil.request2Json(request));
    }
}
